<?php
/**
 * Introduction: 真实仓库表  stock_stocks
 */

namespace Common\Model;


use Think\Model;

class StockStocksModel extends Model
{
    //定义数据库连接信息
    protected $connection;

    //定义表名
    protected $tableName = 'stock_stocks';

    /**
     * 初始化
     */
    public function _initialize()
    {

        $this->connection = C('Databaese');

    }
    /**
     * 获取真实仓库
     * @param unknown $shopId
     * @param unknown $productId
     */
    public function getStocksByShopIdWithProductId($shopId, $productId)
    {
        $record = $this->where("productid=$productId and shopid=$shopId")->field("stocks")->find();
        if (is_null($record)) {
            return 0;
        }
        return $record['stocks'];
    }

    /**
     * 获取已卖未出库的数量
     * @param unknown $shopId
     * @param unknown $productId
     */
    public function getSoldNum($shopId, $productId)
    {
        $record = $this->where("productid=$productId and shopid=$shopId")->field("sold_num")->find();
        if (is_null($record)) {
            return 0;
        }
        return $record->sold_num;
    }

    /**
     * 更新商品已售数量
     * @param unknown $shopId
     * @param unknown $skuId
     * @param unknown $num
     */
    public function updateSoldNum($shopId,$skuId,$num){
        $sold_num = $this->where("productid=$skuId and shopid=$shopId")->find();
        $res = $this->where("productid=$skuId and shopid=$shopId")->save(['cold_num'=>$sold_num['sold_num']+$num]);

        return $res ? true : false;
    }
}